﻿using System;
using System.Windows.Data;

using GroupChallenge.Helpers;
using GroupChallenge.Web;

namespace GroupChallenge.Converters
{
    public class PlayerIdToPlayerImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            object ret = value;

            if (value is int)
            {
                int playerId = (int)value;
                if (playerId > 0)
                {
                    string imageFileName = string.Format("Player_{0}.jpg", playerId);

                    string fullFilePath = string.Format("{0}GroupChallenge/PlayerImages/{1}",
                        Utils.GetHostUrl(true),
                        imageFileName);

                    PlayerImage playerImage = new PlayerImage()
                    {
                        PlayerId = playerId,
                        ImageSourceUrl = fullFilePath,
                        ImageFileName = imageFileName,
                        ImageSourceUri = new Uri(fullFilePath)
                    };

                    ret = playerImage;
                }
            }

            return ret;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
